﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QMSDAL;

namespace QuaManSys.InnerInfo
{
    public partial class InnerMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //判断用户是否是非法访问
            if (Session["CurUser"] != null)
            {
                //从session中取出当前用户信息并显示
                CurUser user = (CurUser)Session["CurUser"];
                lblUserName.Text = user.name;
                lblUserDepartment.Text = user.department;
                if (!IsPostBack) BindMenu(user);
            }
            else
            {
                //跳转到登录页面
                Response.Redirect("~/Login.aspx");
            }
        }
        //注销
        protected void LinkButtonOut_Click(object sender, EventArgs e)
        {
            //清空session中的数据
            Session["CurUser"] = null;
            Response.Redirect("~/Login.aspx");
        }

        private void BindMenu(CurUser user)
        {
            using (var entity = new QuaManSysEntities())
            {
                var queryRoles = ((from q in entity.SysRoleSysDept
                                   where q.Dept == user.department
                                   select q.SysRoleId).Union
                                          (from s in entity.SysRoleSysPerson
                                           where s.SysPersonId == user.employeeId
                                           select s.SysRoleId)).Distinct();

                var queryMenu = (from q in entity.SysMenu
                                 join p in entity.SysMenuSysRoleSysOperation
                                 on q.Id equals p.SysMenuId
                                 join r in queryRoles
                                 on p.SysRoleId equals r
                                 select q).Distinct();
                var queryParentMenu = (from q in queryMenu
                                       where q.ParentId == null
                                       select q).Distinct();
                //MenuItem newMenuItem1 = new MenuItem("主页", null, null, "MainPage.aspx");
                //NavigationMenu.Items.Add(newMenuItem1);
                foreach (var item in queryParentMenu)
                {
                    MenuItem newMenuItem = new MenuItem(item.Name, item.Id, "", item.Url);
                    NavigationMenu.Items.Add(newMenuItem);
                    //AddChildMenuItems(item.Id);
                    var queryChildMenu = (from q in queryMenu
                                          where q.ParentId == item.Id
                                          select q).Distinct();
                    foreach (var childItem in queryChildMenu)
                    {
                        MenuItem newChildMenuItem = new MenuItem(childItem.Name, childItem.Id, "", childItem.Url);
                        newMenuItem.ChildItems.Add(newChildMenuItem);
                    }
                }

                var queryInnerMenu = (from q in queryMenu
                                      where q.ParentId == "1"
                                      select q).Distinct();
                foreach (var item in queryInnerMenu)
                {
                    MenuItem newMenuItem = new MenuItem(item.Name, item.Id, "", item.Url);
                    InnerMenu.Items.Add(newMenuItem);
                }
                //MenuItem newMenuItem2 = new MenuItem("关于");
                //NavigationMenu.Items.Add(newMenuItem2);
                //ShowReminder(user);
            }
        }
    }
}